<?php
require "./config/dbconfig.php";
require "./common/function.php";

//接收post提交的参数
if($_POST){
    //1、填写（获取）信息
    $username = $_REQUEST['username'];
    $pwd = $_REQUEST['pwd'];
    $phone = $_REQUEST['phone'];
    $age = $_REQUEST['age'];
    $sex = $_REQUEST['sex'];
    $roleId = $_REQUEST['role'];
//2、校验是否为空
    if (empty($username)){
        $errMsg = [false,'用户名不能为空'];
        require "html/register.html";
        exit;
    }
    if (empty($pwd)){
        $errMsg = [false,'密码不能为空'];
        require "html/register.html";
        exit;
    }
//    密码进行md5大写加密
    $pwd = strtoupper(md5($pwd));

    if (empty($roleId)){
        $errMsg = [false,'角色不能为空'];
        require "html/register.html";
        exit;
    }
    if (empty($age)){
       $age=0;
    }
    if (empty($sex)){
        $sex=0;
    }
    if (empty($phone)){
        $phone=13800000000;
    }
//    判断用户名是否已经被注册
    $selSql="select * from t_user where username='$username'";
    $selResult = getMySql($selSql,"查询数据失败");
    if($selResult){
        if(mysqli_num_rows($selResult)){
            $errMsg = [false,'用户名已存在，不能重复注册'];
            require "html/register.html";
            exit;
        }
    }

//3、存数据库
    $sql ="insert into `t_user`(`username`,`password`,`iphone`,`sex`,`age`,`role_id`) values ('$username','$pwd','$phone','$sex','$age','$roleId')";
    getMySql($sql,"插入数据失败！");
//4、注册成功
    $errMsg = [true,'注册成功'];
    require "html/register.html";
    exit;
}
//进入注册页面
require "html/register.html";
exit;

